package com.zyzd.baseprojectofvolly.utils.volley;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

/**
 * 图片的内存缓存
 *
 * @author lxj
 */
public class MemeryLruCache implements ImageCache {
    LruCache<String, Bitmap> lruCache;
    //app可用内存的8分之1
    int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);

    public MemeryLruCache() {
        lruCache = new LruCache<String, Bitmap>(maxSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getByteCount() * bitmap.getHeight();
            }
        };
    }

    @Override
    public Bitmap getBitmap(String url) {
        return lruCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        lruCache.put(url, bitmap);
    }

}
